import getLastProto from './utils/getLastProto';
import extendAction from './utils/extendAction';

export default (setData, getData) => {
    return (actions) => {
        let actionInstance = getData('actionInstance');
        let modelsInstance = getData('modelInstance');

        Object.keys(actions).forEach((actionName) => {
            if (!actionInstance[actionName] || !actionInstance[actionName].lock) {
                let Action = actions[actionName];
    
                extendAction(Action, {
                    actions: actionInstance,
                    models: modelsInstance
                });
    
                let action = new Action({
                    actions: actionInstance,
                    models: modelsInstance
                });
    
                action.actions = actionInstance;
                action.models = modelsInstance;
    
                actionInstance[actionName] = action;
            }
        });

        setData('actionInstance', actionInstance);
    }
}